package com.crrc.dataparse.parse.TemplateAlg;

import com.crrc.dataparse.entity.*;
import com.crrc.dataparse.parse.ITemplateParser;

import java.util.List;

/*
    协议体处理方案
    牵引单频率参数包使用
 */
public class SFTemp implements ITemplateParser {
    private static final int BASE_INFO_PARAMATER = 1;
    private static final int WORK_STATUS_PARAMETER = 2;
    private static final int SUBTEMPLATE_PARAMETER = 3;
    private static final int NULL_PARAMETER = 4;
    private static final int BASE_INFO_PARAMETER_Line = 7;
    private static final int BASE_INFO_PARAMETER_Car = 8;
    private static final int BASE_INFO_PARAMETER_Terminal = 9;
    private static final int BASE_INFO_PARAMETER_Time = 10;
    private static final int BASE_INFO_PARAMATER_TrainType = 11;

    public SFTemp() {
    }

    @Override
    public int templateParse(String packetData, int offset, Template template, PaserInfo paserInfo, ParsedResult pdp) throws Exception {
        List<Tem2TemPara> tempList = template.getTem2TemParaList();
        TemplatePara tempPara;

        for(int i = 0; i < tempList.size(); ++i) {
/*            if (offset + 8 >= packetData.length()) {
                offset = -1;
                break;
            }*/

            //int num = Integer.parseInt(packetData.substring(offset, offset + 16), 2);
            int num = 1;
            //offset += 16;
            tempPara = ((Tem2TemPara)tempList.get(i)).getTemplatePara();
            offset = this.getPara2(tempPara, packetData, pdp, offset, num);

            //System.out.println("i = " + i);
            //System.out.println("offset = " + offset);
        }
        return offset;
    }

    public int getPara2(TemplatePara tempPara, String packetData, ParsedResult pdp, int offset, int num) throws Exception {
        int paraType = tempPara.getParameterType();
        int length = tempPara.getLength();

        if (paraType == NULL_PARAMETER) {
            offset += length;
            return offset;
        }

        if (paraType != SUBTEMPLATE_PARAMETER) {
            if (paraType == BASE_INFO_PARAMATER) {
                for(int i = 0; i < num; ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            } else if (paraType == WORK_STATUS_PARAMETER && num != 0) {

                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addSFWorkStatus(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            } else if (paraType == BASE_INFO_PARAMETER_Line) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                    pdp.setlineNo(tempPara.getParameterID());
                }
            } else if (paraType == BASE_INFO_PARAMETER_Car) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                    pdp.setmetroNo(tempPara.getParameterID());
                }
            } else if (paraType == BASE_INFO_PARAMETER_Terminal) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            } else if (paraType == BASE_INFO_PARAMETER_Time) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            } else if (paraType == BASE_INFO_PARAMATER_TrainType) {
                for(int i = 0; i < num && offset + length <= packetData.length(); ++i) {
                    String tempParaValue = packetData.substring(offset, offset + length);
                    pdp.addBaseInfo(tempPara.getParameterID(), tempParaValue);
                    offset += length;
                }
            }
        }
        return offset;
    }
}
